package com.middlegroup.pay.web.api;

import com.middlegroup.common.model.PayRequest;
import com.middlegroup.common.model.RefundRequestDto;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.pay.bind.SessionMerchantCode;
import com.middlegroup.pay.bind.SessionSubjectCode;
import com.middlegroup.pay.service.PayService;
import com.middlegroup.pay.service.PaypalService;
import com.middlegroup.pay.service.WechatPayH5Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author chengweiwei
 * @date 2019/8/7
 */
@Controller
@Slf4j
public class PayController extends BaseController {

    @Resource
    private PayService payService;
    @Autowired
    private PaypalService paypalService;
    @Autowired
    private WechatPayH5Service wechatPayH5Service;

    /**微信,支付宝，paypal支付
     curl -i 'http://10.0.0.180:9090/pay' -d 'state=客户端预留字段&notifyUrl=通知回调url&channel=wx_h5&ip=116.226.45.158&amount=0.01&merchantBillNo=merchantBillNo201908070001&subjectCode=S000001&merchantCode=M000001'
     curl -i 'http://10.0.0.180:9090/gateway' -d 'merchantCode=M000001&method=pay
     &reqData=p/w3CwPXRCAGFKGpfrjFbb+SCQWuxKr8pilgZkvu5qQ9uqka+HKD3NUUHzl3G2ytxV6xxHoBABrOj12hkNx8j/KlTi5MK8e0h8pUvXg2SevhqQ27U5US8VgD1pSiq+5Zqnq7t1lVSpOS5bgWCJ+0IztOCJORfz6tDkOfkugcbZIylhmgWW6KzvyDW3+QeVktF/+jPSjYKrJIanuLqmWgIYlpxDI2Zl7YmUHZR3C9Q3n3778ksXdOwWUpMe9llKlMFQHgtQTAuVnoT/HieTg8FIjxXNcOfgb4yYeYPXS7mauhCcO1R12Phvc68++jWpfQ
     &sign=ySMZKtX4Es9RhhFS3heuZpmbpC10Z8nNk5VnHA4GXEoRv2f1cRTZVxDRChliFNjQifsceVWrSjnk9Q0MYfnq7uxK1DA1PBfowl78F4qUv2+UShTEI27GMtacvJSsBobKwhYWhQ1HH0SF19F317V1zd81+YcFlfcM3XKm27fFPZA=
     &aesKey=KGlvrZ/eC1G2BxprfUDqzWyVaieqmb93W2Hi4Eu0AMqLIO1NLcI1e3WN91YIq0+bI+DHHjauwRX+BQ96d+FcZwVSR76LL4Pc/BFIut5L+zZVYILrMa5KlMKK4pvIkXx+l9YYm5q8/jxbfcT+vgBxFZGVvzAqcmyzOQkzaCWPOAw='
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "pay")
//    ResponseEntity pay(@Valid PayRequest request, @SessionMerchantCode String merchantCode, @SessionSubjectCode String subjectCode) {
    ResponseEntity pay(@Valid PayRequest request, @SessionSubjectCode String subjectCode, @SessionMerchantCode String merchantCode) {
        request.setSubjectCode(subjectCode);
        request.setMerchantCode(merchantCode);
        TransResult transResult = payService.pay(request);
        return new ResponseEntity(ResponseEntity.STATUS_OK, transResult);
    }

    /**
     * 退款
     * @param request
     * @param subjectCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "refund")
    ResponseEntity refund(@Valid RefundRequestDto request,  @SessionSubjectCode String subjectCode, @SessionMerchantCode String merchantCode) {
        request.setSubjectCode(subjectCode);
        request.setMerchantCode(merchantCode);
        TransResult transResult = payService.refund(request);
        return new ResponseEntity(ResponseEntity.STATUS_OK, transResult);
    }
    /**
     * 确认支付(用来第三方回调)
     * @param paymentId
     * @param payerId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "pay/success")
    public String  successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId){
        String url = paypalService.executePayment(paymentId, payerId);
        return "redirect:" + url;
    }
}
